﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.DB;
using System.Data;
using MaxLeaf.Common;

namespace MaxLeaf.DAL.K3
{
    /*
     * SELECT 
vw_ICPrcPly_CTtoI.FID AS FInterID2,vw_ICPrcPly_CTtoI.FItemID,t_ICItem.FNumber AS FItemID_FNDName,t_ICItem.FTrack AS FItemID_Track,t_ICItem.FBatchManager AS FItemID_BatchNoManage,t_ICItem.FQtyDecimal AS FItemID_FQtyDecimal,
t_ICItem.FPriceDecimal AS FItemID_FPriceDecimal,t_ICItem.FUnitGroupID AS FItemID_UnitGroupID,t_ICItem.FISKFPeriod AS FItemID_FISKFPeriod,t_ICItem.FNumber AS FItemID_DSPName,vw_ICPrcPly_CTtoI.FItemID AS FBaseProperty,t_ICItem.FName AS FBaseProperty_DSPName,
vw_ICPrcPly_CTtoI.FRelatedID,t_SubMessage1.FID AS FRelatedID_FNDName,t_SubMessage1.FID AS FRelatedID_DSPName,vw_ICPrcPly_CTtoI.FRelatedID AS FBaseProperty1,t_SubMessage1.FName AS FBaseProperty1_DSPName,vw_ICPrcPly_CTtoI.FItemID AS FBaseProperty2,
t_ICItem.FModel AS FBaseProperty2_DSPName,vw_ICPrcPly_CTtoI.FAuxPropID,t_AuxItem.FNumber AS FAuxPropID_FNDName,t_AuxItem.FName AS FAuxPropID_DSPName,vw_ICPrcPly_CTtoI.FUnitID,t_Measureunit.FNumber AS FUnitID_FNDName,
t_Measureunit.FName AS FUnitID_DSPName,vw_ICPrcPly_CTtoI.FBegQty,vw_ICPrcPly_CTtoI.FEndQty,vw_ICPrcPly_CTtoI.FCuryID,t_Currency.FNumber AS FCuryID_FNDName,t_Currency.FScale AS FCuryID_Decimal,
t_Currency.FName AS FCuryID_DSPName,vw_ICPrcPly_CTtoI.FPriceType,t_SubMessage.FID AS FPriceType_FNDName,t_SubMessage.FName AS FPriceType_DSPName,vw_ICPrcPly_CTtoI.FPrice,vw_ICPrcPly_CTtoI.FLPriceCuryID,
t_Currency1.FNumber AS FLPriceCuryID_FNDName,t_Currency1.FScale AS FLPriceCuryID_Decimal,t_Currency1.FName AS FLPriceCuryID_DSPName,vw_ICPrcPly_CTtoI.FLowPrice,vw_ICPrcPly_CTtoI.FBegDate,vw_ICPrcPly_CTtoI.FEndDate,
vw_ICPrcPly_CTtoI.FLeadTime,vw_ICPrcPly_CTtoI.FNote,vw_ICPrcPly_CTtoI.FChecked,vw_ICPrcPly_CTtoI.FCanSell,vw_ICPrcPly_CTtoI.FLPriceCtrl,vw_ICPrcPly_CTtoI.FNumber AS FParentNumber,
vw_ICPrcPly_CTtoI.FName AS FParentName FROM vw_ICPrcPly_CTtoI  LEFT  JOIN t_ICItem  ON vw_ICPrcPly_CTtoI.FItemID=t_ICItem.FItemID AND t_ICItem.FItemID<>0
 LEFT  JOIN t_SubMessage t_SubMessage1 ON vw_ICPrcPly_CTtoI.FRelatedID=t_SubMessage1.FInterID AND t_SubMessage1.FInterID<>0
 LEFT  JOIN t_AuxItem  ON vw_ICPrcPly_CTtoI.FAuxPropID=t_AuxItem.FItemID AND t_AuxItem.FItemID<>0
 LEFT  JOIN t_Measureunit  ON vw_ICPrcPly_CTtoI.FUnitID=t_Measureunit.FItemID AND t_Measureunit.FItemID<>0
 LEFT  JOIN t_Currency  ON vw_ICPrcPly_CTtoI.FCuryID=t_Currency.FCurrencyID AND t_Currency.FCurrencyID<>0
 LEFT  JOIN t_SubMessage  ON vw_ICPrcPly_CTtoI.FPriceType=t_SubMessage.FInterID AND t_SubMessage.FInterID<>0
 LEFT  JOIN t_Currency t_Currency1 ON vw_ICPrcPly_CTtoI.FLPriceCuryID=t_Currency1.FCurrencyID AND t_Currency1.FCurrencyID<>0
 INNER  JOIN #tmpID ON #tmpID.FEntryID = vw_ICPrcPly_CTtoI.FID ORDER BY id_num
     * 
     * INSERT INTO IcPrcPlyEntry(FIndex,FItemID,FRelatedID,FAuxPropID,FInterID,FUnitID,FBegQty,FEndQty,FCuryID,FPriceType,FPrice,FBegDate,FEndDate,FLeadTime,FNote,Fchecked,FFlagSave) 
     * Values(1,28166,20011,0,2,11249,1,2,1,0,3,'2011-01-16','2100-01-01',0,'',0,'{2BD2B3A4-1CA2-4950-9E53-D5EF885AE16A}')
     * 
     */

    /// <summary>
    ///价格政策dtl
    /// </summary>
    public class ICPrcPlyEntryDAL : BaseDAL
    {
        public ICPrcPlyEntryDAL()
            : base()
        {
            strTableName = "ICPrcPlyEntry";

            strMapKeyFieldName = "FEntryID";
            strDestRealKeyFieldName = "FCuryID";

            mblnNeedEditKeyValue = false;
            mblnIsIdentity = true;
            blnMapKeyUseOtherKeyFieldsCond = false;

            strFieldNames = "FInterID,FItemID,FRelatedID,FModel,FAuxPropID,FUnitID,FBegQty,FEndQty,FCuryID,FPriceType,FPrice,FBegDate,FEndDate,FLeadTime,FNote,FChecked,FIndex,FTime,FID,FBase,FBase1,FBegQty_Base,FEndQty_Base,FInteger,FClassTypeID,FBCust,FB2CustCls,FB2Emp,FB2EmpCls,FB2VipGrp,FB2Cust,FB2ItemID,FB2Item,FFlagSave";
        }

        protected override void UpdateOtherData(DBAccess dbOrgCon, DBAccess dbCon, DataRow drOrgData, DataRow drData)
        {
            new ICPrcPlyEntrySpecDAL().UpdateSpec(dbOrgCon, dbCon, drOrgData, drData);
            
        }
        public override void Update(DBAccess dbOrgCon, DBAccess dbDestCon, string strKeyValue)
        {
            KeyValuePair<string, IDataParameter[]> kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "TableName", strTableName }, { "OrgKeyValue", strKeyValue } });
            string strSql = string.Format("select DestKeyValue from mkeymap where {0}", kvp.Key);

            string strFEntryID = dbDestCon.GetValue(strSql, kvp.Value);
            if (strFEntryID.Length > 0)
            {

            }
            else
            {
                //FRelatedID=类别id，有主库分发,源和目标的可能不一样
                DataRow drOrgData = dbOrgCon.GetTableDataFirstRow(strTableName, new Dictionary<string, object>() { { strMapKeyFieldName, strKeyValue } });
                dictOtherKeyFieldsCond = new Dictionary<string, object>() { 
                    {"FInterID",drOrgData["FInterID"]}
                    ,{"FItemID",drOrgData["FItemID"]}
                    ,{"FRelatedID",drOrgData["FRelatedID"]}
                    ,{"FUnitID",drOrgData["FUnitID"]}
                    ,{"FBegQty",drOrgData["FBegQty"]}
                    ,{"FEndQty",drOrgData["FEndQty"]}
                    ,{"FPrice",drOrgData["FPrice"]}
                    ,{"FBegDate",drOrgData["FBegDate"]}
                    ,{"FEndDate",drOrgData["FEndDate"]}
                };
                dictDestOtherKeyFieldsCond = new Dictionary<string, object>() { 
                    {"FInterID",new ICPrcPlyDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FInterID"]))}
                    ,{"FItemID",new ImInvMasterDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FItemID"]))}
                    ,{"FRelatedID",new t_SubMessageDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FRelatedID"]))}
                    ,{"FUnitID",new t_MeasureUnitDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FUnitID"]))}
                    ,{"FBegQty",drOrgData["FBegQty"]}
                    ,{"FEndQty",drOrgData["FEndQty"]}
                    ,{"FPrice",drOrgData["FPrice"]}
                    ,{"FBegDate",drOrgData["FBegDate"]}
                    ,{"FEndDate",drOrgData["FEndDate"]}
                };
            }
            base.Update(dbOrgCon, dbDestCon, strKeyValue);
        }

        protected override void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FItemID", StringComparison.InvariantCultureIgnoreCase))
            {
                //物料
                drDestData[strFieldName] = new ImInvMasterDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));
                
            }
            else if (strFieldName.Equals("FRelatedID", StringComparison.InvariantCultureIgnoreCase))
            {
                //类别
                drDestData[strFieldName] = new t_SubMessageDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));

            }
            else if (strFieldName.Equals("FUnitID", StringComparison.InvariantCultureIgnoreCase))
            {
                //单位
                drDestData[strFieldName] = new t_MeasureUnitDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));

            }
            else if (strFieldName.Equals("FInterID", StringComparison.InvariantCultureIgnoreCase))
            {
                //hdr.FInterID
                drDestData[strFieldName] = new ICPrcPlyDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));

            }
            else
            {
                base.DAL_GetFieldData(dbOrgCon, dbDestCon, strFieldName, drOrgData, drDestData);
            }
        }

    }
}
